<?php
class CartFinishForm extends Form
{
	private $cart=false,$cart_items=false;
	
	function CartFinishForm()
	{
		Form::Form('CartFinishForm');
		
		CGlobal::$website_title = "Thanh toán đơn hàng";
	}
	
	function on_submit()
	{
		if(Cart::$cart && Cart::$cart_items)
		{
			$user_id			=User::id();
			$user_name			=User::user_name();

			$customer_name		=URL::get('customer_name');
			$customer_email		=URL::get('customer_email');
			$customer_phone		=URL::get('customer_phone');
			$customer_address	=URL::get('customer_address');
			$customer_note		=URL::get('customer_note');
			
			$this->checkFormInput("Tên đầy đủ",'customer_name',$customer_name,'str',true,'',2,50);
			$this->checkFormInput("Email",'customer_email',$customer_email,'email',true,'',false,50);
			$this->checkFormInput("Điện thoại liên hệ",'customer_phone',$customer_phone,'str',true,'',9,50);
			$this->checkFormInput("Địa chỉ",'customer_address',$customer_address,'str',true,'',false,500);
			$this->checkFormInput("Ghi chú",'customer_note',$customer_note,'str',false,'',false,500);
			
			if(!$this->errNum)
			{
				$status = 1;
				
				if(Url::get('cart_act') =='send_cart')
				{
					$status = 2;
				}
				
				$arr	=	array(
								'user_id'			=>$user_id,
								'user_name'			=>$user_name,
								'customer_name'		=>addslashes($customer_name),
								'customer_email'	=>addslashes($customer_email),
								'customer_phone'	=>addslashes($customer_phone),
								'customer_address'	=>addslashes($customer_address),
								'customer_note'		=>addslashes($customer_note),
								'time_change'		=>TIME_NOW,
								'status'			=>$status,
								);

				if($status == 2)
				{
					$arr['time_finish'] = TIME_NOW;
				}
				
				if(DB::update('cart',$arr,"id=".Cart::$cart['id'],__LINE__.__FILE__))
				{
					if($status == 2)
					{
						DB::update("cart_item",array('status'=>$status),"cart_id=".Cart::$cart['id']." AND checked=1");//0:Chưa TT; 1: Đã gửi đơn hàng; 2:Đã TT; 3: Hủy đơn hàng
						
						AZLib::my_setcookie('cart_items','',time()+3600*24*365);
			
						URL::redirect_current(array('cmd'=>'success'));
					}
					else
					{
						URL::redirect_current(array('cmd'));
					}
				}
				else 
					$this->setFormError("customer_name","Không cập nhật được, mời bạn thử lại!");
			}
		}
	}
	
	function draw()
	{
		global $display;
		
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$display->add("customer_name",		stripslashes(URL::get('customer_name',Cart::$cart['customer_name'])));
		$display->add("customer_email",		stripslashes(URL::get('customer_email',Cart::$cart['customer_email'])));
		$display->add("customer_phone",		stripslashes(URL::get('customer_phone',Cart::$cart['customer_phone'])));
		$display->add("customer_address",	stripslashes(URL::get('customer_address',Cart::$cart['customer_address'])));
		$display->add("customer_note",		stripslashes(URL::get('customer_note',Cart::$cart['customer_note'])));
				
		//$quantity_total	=0;
		$price_total	=0;
			
		$cart_items = array();
		
		if(Cart::$cart_items)
		{
			foreach (Cart::$cart_items  as $cart_item_id => $cart_item)
			{
				$item = Cart::$items[$cart_item['item_id']];
				
				if(isset(CGlobal::$allCategories[$item['category_id']]))
				{
					$cart_item['url'] = WEB_DIR.CGlobal::$allCategories[$item['category_id']]['nice_name']."/p{$item['id']}/".AZLib::safe_title($item['name']).".html";
				}
				else{	
					$cart_item['url'] = WEB_DIR."p{$item['id']}/".AZLib::safe_title($item['name']).".html";
				}
		
				$cart_item['name'] 		= $item['name'];
				$cart_item['price_num'] = $item['price'];
				
				$cart_item['price'] 	= number_format($item['price'],0,',','.');
				
				if ($item['img_url']){
					$cart_item['img_url'] 		= AZLib::getImageThumb($item['img_url'],120,120,0,$item['img_server']);
				}
				else{
					$item['img_url'] 			=  '';
				}
				
				$cart_item['row_total'] = $cart_item['checked'] ? (number_format($item['price'] * $cart_item['quantity'],null,null,'.')) . ' VNĐ' : '';
				
				$price_total	+=	$cart_item['checked'] ? ($item['price'] * $cart_item['quantity']) : 0;
				//$quantity_total	+=	$cart_item['checked'] ? $cart_item['quantity'] : 0;
				
				$cart_items[$cart_item['id']] = $cart_item;
			}
		}
			
		//$display->add('quantity_total',number_format($quantity_total,null,null,'.'));
		$display->add('price_total', number_format($price_total,null,null,'.'));
		
		$display->add('url_finish',Url::build_current(array('cmd'=>'finish')));
		$display->add('cart_items',$cart_items);
		
		$display->add('cart_id',"Cart_".Cart::$cart['id']);
		
		$this->beginForm();
		$display->output('CartFinish');
		$this->endForm();
	}
}
?>